package com.doge.calcalbum.widgets

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Typeface
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import com.doge.calcalbum.R
import com.doge.calcalbum.utils.getString
import java.util.*

//普通简约弹窗
class SheetDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {
    class Params {
        val menuList: MutableList<Sheet> = ArrayList()
        lateinit var context: Context
    }

    class Sheet(val funName: String, val listener: DialogInterface.OnClickListener?) {
        val textColor: Int = 0xFF3369FF.toInt()
    }

    class Builder(context: Context) {
        private val p: Params = Params()

        init {
            p.context = context
        }

        fun addMenu(
            text: String,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            val bm = Sheet(text, listener)
            p.menuList.add(bm)
            return this
        }

        fun create(): SheetDialog {
            val shadow = true
            val dialog = SheetDialog(
                p.context,
                if (shadow) R.style.Theme_Light_NoTitle_Dialog else R.style.Theme_Light_NoTitle_NoShadow_Dialog
            )
            val window = dialog.window
            val dip1 = (p.context.resources.displayMetrics.density + 0.5f).toInt()
            if (window != null) {
                window.setWindowAnimations(R.style.Animation_Bottom_Rising)
                window.decorView.setPadding(dip1 * 12, 0, dip1 * 12, dip1 * 20)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = lp
                window.setGravity(Gravity.BOTTOM)
            }
            val layContainer = LinearLayout(p.context)
            layContainer.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            layContainer.orientation = LinearLayout.VERTICAL

            val layItems = LinearLayout(p.context)
            layItems.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            layItems.orientation = LinearLayout.VERTICAL
            layItems.setBackgroundResource(R.drawable.shape_ffffff_8)
            val lpItem = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            lpItem.gravity = Gravity.CENTER
            val lpDivider =
                LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp2px(1))
            for (i in p.menuList.indices) {
                val bottomMenu = p.menuList[i]
                val bbm = TextView(p.context)
                bbm.layoutParams = lpItem
                val isFirst = i == 0
                val isLast = i >= p.menuList.size - 1
                bbm.setBackgroundResource(
                    when {
                        isFirst && isLast -> R.drawable.ripple_15000000_8
                        isFirst -> R.drawable.ripple_15000000_8_top
                        isLast -> R.drawable.ripple_15000000_8_bottom
                        else -> R.drawable.ripple_15000000
                    }
                )
                bbm.gravity = Gravity.CENTER
                bbm.text = bottomMenu.funName
                bbm.setTextColor(bottomMenu.textColor)
                bbm.textSize = 16f
                bbm.minHeight = dp2px(55)
                bbm.typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
                bbm.setOnClickListener {
                    dialog.dismiss()
                    if (bottomMenu.listener != null) bottomMenu.listener.onClick(dialog, 0)
                }
                layItems.addView(bbm)
                if (p.menuList.size > 1 && !isLast) {
                    val divider = View(p.context)
                    divider.layoutParams = lpDivider
                    divider.setBackgroundColor(0xffE5E5E5.toInt())
                    layItems.addView(divider)
                }
            }

            layContainer.addView(layItems)
            addCancelButton(dialog, layContainer)

            dialog.setContentView(layContainer)
            val canCancel = true
            dialog.setCanceledOnTouchOutside(canCancel)
            dialog.setCancelable(canCancel)
            return dialog
        }

        private fun dp2px(dpValue: Int): Int {
            return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dpValue.toFloat(),
                p.context.resources.displayMetrics
            ).toInt()
        }


        private fun addCancelButton(dialog: Dialog, container: ViewGroup) {
            val lpItem = ViewGroup.MarginLayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            )
            lpItem.topMargin = dp2px(12)

            val bbm = TextView(p.context)
            bbm.layoutParams = lpItem
            bbm.setBackgroundResource(R.drawable.ripple_sheet_cancel)
            bbm.gravity = Gravity.CENTER
            bbm.text = getString(R.string.cancel1)
            bbm.setTextColor(0xFF999999.toInt())
            bbm.textSize = 16f
            bbm.minHeight = dp2px(55)
            bbm.paint.isFakeBoldText = false
            bbm.setOnClickListener {
                dialog.dismiss()
            }
            container.addView(bbm)
        }
    }


}